/*
 * LocalRollUpRangeZT.java
 *
 * Created on January 31, 2005, 2:50 PM
 */

package edu.temple.GUS.MMA.Functions;

import edu.temple.GUS.MMA.DataTypes.*;
import edu.temple.GUS.MMA.Exceptions.MMAFunctionInitException;
import edu.temple.GUS.MMA.util.StatisticalOperator;

/**
 *
 * @author  jeffleon
 */
public class LocalRollUpRangeZT {
    
    /** Creates a new instance of LocalRollUpRangeZT */
    public LocalRollUpRangeZT() {}
    
    // Roll up a hyper cube
    public MMAGrid execute( MMAHyperCube hCube ) throws MMAFunctionInitException
    {
        if (hCube == null)
            throw new MMAFunctionInitException(this.getClass().toString() + " Hyper cube is not initialized.");    

        // Initialze the dimensions of the hypercube and the noData value
        int x = hCube.getX();
        int y = hCube.getY();
        int z = hCube.getZ();
        int t = hCube.getT();
        float noData = hCube.getNoData(); 

        MMAGrid outputGrid = new MMAGrid(x,y);
        float resultData[][] = new float[y][x];
        float tempZ[] = new float[z];
        float tempT[] = new float[t];
        
        for ( int j=0; j <y; j++)
            for ( int i=0; i <x; i++)
            {
                for ( int l=0; l <t; l++)
                {
                    // Roll up in z-direction
                    for ( int k=0; k <z; k++)
                        if (hCube.getElement(i,j,k,l) == noData)
                        {
                            resultData[j][i] = noData;
                            break;
                        }
                        else
                            tempZ[k] = hCube.getElement(i,j,k,l);
                    
                    if (resultData[j][i] == noData)
                        break;
                    else
                        tempT[l] = StatisticalOperator.max(tempZ,z) - StatisticalOperator.min(tempZ);
                }
                
                if (resultData[j][i] != noData)
                    resultData[j][i] = StatisticalOperator.max(tempT,t) - StatisticalOperator.min(tempT);
            }
        
        outputGrid.putData(resultData);
        outputGrid.putNoData(noData);
        return outputGrid;
    }
}
